package com.ysd.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBManger {
	private final static String DRIVER = "com.mysql.jdbc.Driver";
	private final static String URL="jdbc:mysql://localhost:3306/qiuxieshangcheng";
	private final static String USER="root";
	private final static String pwd="ramin";
	
	public static Connection getConnection() {//������������������쳣����null
		//1��������
		try {
			Class.forName(DRIVER);
			//2��������
			Connection conn=DriverManager.getConnection(URL,USER,pwd);
			return conn;
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	//ִ�з���
	
	public static int executeUpdate(String sql) {
		Connection conn=getConnection();
		Statement st=null;
		//3����Statementִ�ж���ִ��sql���
		try {
			st= conn.createStatement();
			int n = st.executeUpdate(sql);
			return n;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}finally {
			closeAll(conn,st,null);
		}
		
		
	}
	
	
	
	
	//6�ر���
	public static void closeAll(Connection conn,Statement st,ResultSet rs) {
		try {
			if(rs!=null) {
				rs.close();
			}
			if(st!=null) {
				st.close();
			}
			if(conn!=null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();//��ӡ�쳣��Ϣ
		}
	
	}
}
